<template>
  <div>
    <!-- 表格 -->
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      class="table-container"
      element-loading-text="加载中..."
      border
      fit
      highlight-current-row
      stripe
      @sort-change="handleSortChange"
    >
      <el-table-column
        type="index"
        width="50"
        :align="'center'"
        label="序列号"
        :width="80"
      />
      <el-table-column
        v-for="(value, key) in columns"
        v-if="value.show"
        :key="key"
        :label="value.label"
        :align="value.center ? 'center' : 'left'"
        :prop="key"
        sortable
      >
        <template slot-scope="scope">
          <span v-if="value.label==='报警数值'" :class="classChagne(scope.row)">{{ scope.row.data }}</span>
          <span v-else-if="value.label==='报警级别'" :class="classChagne(scope.row)">{{ rankChange(scope.row) }}</span>
          <span v-else>{{ scope.row[key] }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            plain
            size="mini"
            type="primary"
            icon=""
            @click="handleEditClick(scope.row)"
          >查看详情</el-button>
          <el-button
            plain
            size="mini"
            type="primary"
            icon=""
            @click="handlePosClick(scope.row)"
          >定位</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表单 -->
    <el-dialog
      :title="'查看报警信息'"
      :visible.sync="formShow"
      :close-on-click-modal="true"
      :close-on-press-escape="false"
      :before-close="handleBeforeClose"
    >
      <el-form ref="form" :model="row" label-width="100px">
        <el-form-item label="传感器类型">
          <!-- <el-input v-model="row.warningType" :disabled="true"/> -->
          <p style="text-align: center">{{ row.deviceName }}-{{ row.type }}</p>
        </el-form-item>
        <el-form-item label="报警数值">
          <p style="text-align: center">{{ row.data }}</p>
          <!-- <el-input v-model="row.warningMessage" :disabled="true" /> -->
        </el-form-item>
        <el-form-item label="报警时间">
          <p style="text-align: center">{{ row.createTime }}</p>
          <!-- <el-input v-model="row.timeS" :disabled="true"/> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button @click="handleBeforeClose">关闭</el-button> -->
        <!-- <el-button v-if="isCreate" type="primary" @click="handleCreateClick">创 建</el-button>
        <el-button v-else type="primary" @click="handleUpdateClick">更 新</el-button> -->
      </div>
    </el-dialog>
    <!-- 分页 组件 -->
    <el-pagination
      :current-page.sync="listQuery.page"
      :page-size="listQuery.size"
      :total="total"
      :page-sizes="[5, 10, 20, 30, 50]"
      class="pagination-container"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { versionUpdate } from '@/api/device'
import { getErrorData } from '@/api/chart'
import { warningMsg } from '@/api/warning'
import { maintain } from '@/api/dashboard'
import { getFirmwareList } from '@/api/firmware'
import { toLowerCase } from '@/utils'
export default {
  data() {
    return {
      list: [

      ],
      listLoading: false,
      listQuery: {
        page: 1,
        size: 10,
        order: 'id',
        title: ''
      },
      total: 0,
      searchShow: false,
      formShow: false,
      isCreate: true, // true创建, false:更新
      headerShow: false,
      isIndeterminate: true,
      checkAll: true,
      columns: [],
      row: {},
      firmwareList: [],
      versionForm: {
        deviceId: '',
        fid: ''
      },
      rule: {
        fid: [{ required: true, message: '请选择版本', trigger: 'change' }]
      },
      firmwareShow: false,
      checkAdd: false,
      checkEdit: false,
      checkDelete: false,
      typeOptions: [
        {
          value: 0,
          label: '测距点'
        },
        {
          value: 1,
          label: '报警器'
        },
        {
          value: 2,
          label: '固定点'
        }
      ]
    }
  },
  computed: {
    header: {
      set: function(v) {
        Object.keys(this.columns).forEach((key) => {
          this.columns[key].show = false
          v.forEach((i) => {
            if (key === i) {
              this.columns[key].show = true
            }
          })
        })
        const checkedCount = v.length
        this.checkAll = checkedCount === Object.keys(this.columns).length
        console.log(this.checkAll)
        this.isIndeterminate =
          checkedCount > 0 && checkedCount < Object.keys(this.columns).length
        console.log(
          this.isIndeterminate,
          v.length,
          Object.keys(this.columns).length
        )
        return Object.keys(this.columns).filter((i) => this.columns[i].show)
      },
      get: function() {
        var array = Object.keys(this.columns).filter(
          (i) => this.columns[i].show
        )
        return array
      }
    }
  },
  mounted() {
    this.fetchData()
    this.columns = warningMsg()
    var checkedCount = this.header.length
    this.checkAll = checkedCount === Object.keys(this.columns).length
    this.isIndeterminate =
      checkedCount > 0 && checkedCount < Object.keys(this.columns).length
  },
  methods: {
    rankChange(row) {
      let a = null
      switch (row.rank) {
        case 1:a = '一级'; break
        case 2:a = '二级'; break
        case 3:a = '三级'; break
      }
      return a
    },
    classChagne(row) {
      let a = null
      switch (row.rank) {
        case 1:a = 'blue'; break
        case 2:a = 'violet'; break
        case 3:a = 'red'; break
      }
      return a
    },
    // 跳转
    handlePosClick(row) {
      this.$router.push({ name: 'Chart', params: { lng: Number(row.lng), lat: Number(row.lat) }})
    },
    fetchData() {
      getErrorData({
        pageNum: this.listQuery.page,
        pageSize: this.listQuery.size
      }).then(res => {
        console.log(res)
        if (res.status === 200) {
          this.list = res.data.pageList
          this.total = res.data.total
        }
      })
    },
    // <<<< 工具栏相关

    handleRefreshClick() {
      this.$message({
        message: '刷新成功',
        type: 'success',
        duration: 800
      })
      this.fetchData()
    },
    handleSearchShowChange() {
      this.searchShow = !this.searchShow
    },
    handleAddClick() {
      this.$router.push({
        name: 'SceneWarning'
      })
    },
    // 升级版本
    handleUpdate(data) {
      getFirmwareList({ deviceId: data.id }).then((res) => {
        if (res.ok) {
          this.firmwareList = res.data
        }
      })
      this.versionForm = {
        deviceId: data.id,
        fid: data.fid
      }
      this.firmwareShow = true
    },
    handleClickUpdate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { deviceId, fid } = this.versionForm
          versionUpdate({
            deviceId,
            fid
          }).then((res) => {
            if (res.ok) {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 800
              })
              this.firmwareShow = false
              this.fetchData()
            }
          })
        }
      })
    },
    // 维护模式的开启与关闭 1 关闭状态 0 开启状态
    clickMaintain(data) {
      maintain({
        deviceId: data.id,
        status: data.isMain ? 0 : 1
      }).then((res) => {
        if (res.ok) {
          this.$message({
            type: 'success',
            message: res.data
          })
          this.fetchData()
        }
      })
    },
    handleHeaderClick() {
      this.headerShow = true
    },
    handleEditClick(row) {
      this.isCreate = false
      this.formShow = true
      this.row = Object.assign({}, row)
    },
    handleBeforeClose() {
      this.row = {}
      this.formShow = false
      this.$refs.form.resetFields()
    },
    // >>>> 表格相关

    // <<<< 分页相关
    handleSizeChange(val) {
      console.log(val)
      this.listQuery.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.fetchData()
    },
    handleSortChange(val) {
      this.listQuery.asc = val.order !== 'descending'
      this.listQuery.order = toLowerCase(val.prop)
      this.fetchData()
    }
  }
}
</script>

<style lang="scss" scoped>
.blue{color: blue;}
.violet{color: yellow;}
.red{color: red;}
</style>
